package com.mengtian_tech.smart_lab.service.impl.mail;

import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.core.common.hibernate.qbc.HqlQuery;
import org.jeecgframework.core.common.hibernate.qbc.PageList;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import com.mengtian_tech.smart_lab.entity.system.JformInnerMailEntity;
import com.mengtian_tech.smart_lab.entity.system.JformInnerMailReceiverEntity;
import com.mengtian_tech.smart_lab.entity.system.MailState;
import com.mengtian_tech.smart_lab.entity.system.TSUser;
import com.mengtian_tech.smart_lab.service.mail.MailService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service("MailService")
@Transactional
public class MailServiceImpl extends CommonServiceImpl implements MailService {

	@Override
	public boolean sendMail(TSUser receive, String content, String title,String mailType) {
		try{
			JformInnerMailEntity mail = new JformInnerMailEntity();
			mail.setTitle(title);
			mail.setContent(content);
			mail.setCreateDate(new Date());
			mail.setCreateName("系统邮件");			
			mail.setId(UUID.randomUUID().toString());
			mail.setReceiverNames("");
			mail.setStatus("01");
			
			save(mail);
			
			JformInnerMailReceiverEntity mailReceiver = new JformInnerMailReceiverEntity();
			mailReceiver.setJMail(mail);
			mailReceiver.setIsAuditMsg(false);
			mailReceiver.setCreateDate(new Date());
			mailReceiver.setStatus(MailState.MAILRECEIVER_STATUS_UNREAD); 
			mailReceiver.setTSUser(receive);
			mailReceiver.setMailType(mailType);
			save(mailReceiver);
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public boolean sendAuditMail(TSUser receive, String content, String title,String mailType) {
		try{
			JformInnerMailEntity mail = new JformInnerMailEntity();
			mail.setTitle(title);
			mail.setContent(content);
			mail.setCreateDate(new Date());
			mail.setCreateName("系统邮件");			
			mail.setId(UUID.randomUUID().toString());
			mail.setReceiverNames("");
			mail.setStatus("01");
			
			save(mail);
			
			JformInnerMailReceiverEntity mailReceiver = new JformInnerMailReceiverEntity();
			mailReceiver.setJMail(mail);
			mailReceiver.setIsAuditMsg(true);
			mailReceiver.setCreateDate(new Date());
			mailReceiver.setStatus(MailState.MAILRECEIVER_STATUS_UNREAD); 
			mailReceiver.setTSUser(receive);
			mailReceiver.setMailType(mailType);
			save(mailReceiver);
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public PageList getReceiveMailList(TSUser receive,String title,String senderName,int pageNo,int pageSize) {
 		StringBuffer sqlb =new StringBuffer("from JformInnerMailReceiverEntity where TSUser.id ='"+receive.getId() 
				+"'and JMail.status !='"+MailState.MAIL_STATUS_UNSEND+"' order by createDate desc");

		if(StringUtils.isNotEmpty(senderName)){
			sqlb.append(" and JMail.createName like '%"+senderName+"%'");
		}
		
		if(StringUtils.isNotEmpty(title)){
			sqlb.append(" and JMail.title like '%"+title+"%'");
		}
		
		//收件状态不为删除，且邮件为已发送状态
		HqlQuery query = new HqlQuery(sqlb.toString(),null);
		query.setPageSize(pageSize);
		query.setCurPage(pageNo);
		
		PageList receivers  =  getPageList(query,false);
		return receivers;
	}
	
	@Override
	public PageList getUnReadMailList(TSUser receive,int pageNo,int pageSize,boolean isAuditMsg) {
 		StringBuffer sqlb =new StringBuffer("from JformInnerMailReceiverEntity where TSUser.id ='"+receive.getId() 
				+"'");
 
		sqlb.append(" and status = '00' ");
		
		if(isAuditMsg){
			sqlb.append(" and isAuditMsg=true  ");			
		}else{
			sqlb.append(" and (isAuditMsg = null or isAuditMsg = false )  ");	
		}
		
		sqlb.append(" order by createDate desc");
		
		//收件状态不为删除，且邮件为已发送状态
		HqlQuery query = new HqlQuery(sqlb.toString(),null);
		query.setPageSize(pageSize);
		query.setCurPage(pageNo);
		
		PageList receivers  =  getPageList(query,false);
		return receivers;
	}
	 
}